package com.egao.common.publicly;

import cn.hutool.core.bean.BeanUtil;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.world.WorldGameConstants;
import com.egao.common.module.world.entity.WorldGame;
import com.egao.common.module.world.entity.WorldGameStudent;
import com.egao.common.module.world.service.WorldGameService;
import com.egao.common.module.world.service.WorldGameStudentService;
import com.egao.common.module.world.vo.WorldGameDataVO;
import com.egao.common.module.world.vo.WorldGamePubliclyVO;
import com.egao.common.module.world.vo.WorldGameStudentBtn;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 济世杯公开信息
 * @author root
 */
@Api(tags = "济世杯公开信息")
@RestController
@RequestMapping("${api}")
public class PubliclyWorldController extends BaseController {

    @Autowired
    private WorldGameService worldGameService;
    @Autowired
    private WorldGameStudentService worldGameStudentService;

    @ApiOperation(value = "分页列表")
    @GetMapping("/publicly/world-game/page")
    public PageResult<WorldGamePubliclyVO> page(HttpServletRequest request) {
        PageParam<WorldGamePubliclyVO> pageParam = new PageParam<>(request);
        return new PageResult<>(worldGameService.listPubliclyPage(pageParam), pageParam.getTotal());
    }

    @ApiOperation(value = "详情")
    @GetMapping("/publicly/world-game/detail")
    public JsonResult detail(Integer id) {
        WorldGame worldGame = worldGameService.getById(id);
        if (null == worldGame) {
            return JsonResult.error(404, "未找到信息");
        }

        if (WorldGameConstants.RELEASE_STATE_NO.equals(worldGame.getReleaseState())) {
            return JsonResult.error(404, "未找到信息");
        }

        worldGameService.updateHits(worldGame);

        WorldGameStudentBtn worldGameStudentBtn = worldGameStudentService.initBtn(id, getLoginUser());
        return JsonResult.ok().setData(worldGame).put("btn", worldGameStudentBtn);
    }

    @OperLog(value = "济世杯", desc = "学生报名", result = true)
    @ApiOperation(value = "济世杯学生报名")
    @PostMapping("/publicly/world-game/apply")
    public JsonResult apply(@RequestBody WorldGameStudent worldGameStudent) {
        return worldGameStudentService.apply(worldGameStudent, getLoginUser());
    }

    @OperLog(value = "济世杯", desc = "学生取消报名", result = true)
    @ApiOperation(value = "济世杯学生取消报名")
    @PutMapping("/publicly/world-game/cancel")
    public JsonResult cancel(@RequestBody WorldGameStudent worldGameStudent) {
        return worldGameStudentService.cancel(worldGameStudent);
    }

}
